package parser;

import java.io.StringWriter;

import org.jrubyparser.NodeVisitor;
import org.jrubyparser.ast.*;
import org.jrubyparser.rewriter.ReWriteVisitor;

import parser.Storage;

public class RoutesVisitor implements NodeVisitor{

	Storage storage = Storage.getInstance();
	private ControlFlowGraph rootCfg;
	private static CFGNode cfgNode;
	private static String type, code;
	
	public void visit(Node ast)
	{
		RoutesVisitor visitor = new RoutesVisitor();
		ast.accept(visitor);
	}
	public String toCode(Node astNode)
	{
		String code=null;
		try{
			StringWriter writer = new StringWriter();
			ReWriteVisitor reVisitor = new ReWriteVisitor(writer, "");
			astNode.accept(reVisitor);
			code = writer.toString();
		}catch(StringIndexOutOfBoundsException siobe){
	        System.out.println("invalid input" );
	        siobe.printStackTrace();
	    }
		
		return code;
	}
	
	private ControlFlowGraph getRootCfg() {
		return storage.getCfgFile().get("root");
	}
	private void setRootCfg(ControlFlowGraph rootCfg) {
		this.rootCfg = rootCfg;
	}
	public void setIncomingNodes(CFGNode node)
	{

		CFGNode last=new CFGNode();
		//CFGNode befLast=new CFGNode();
		last = getRootCfg().getGraphNodes().get(getRootCfg().getGraphNodes().size()-1);
		if(getRootCfg().getGraphNodes().size()==1)
		{
			node.addComingNodes(last);
			last.addOutgoingNodes(node);
		}
		else if(getRootCfg().getGraphNodes().size()>=2)
		{
			if(type=="case"||type=="for"||type=="if"||type=="until"||type=="while")
			{
				node.addComingNodes(last);
				last.addOutgoingNodes(node);
			}
			else if(last.getType().equals("bodywhile")||last.getType().equals("bodyfor")||last.getType().equals("bodyuntil"))
			{
				if(!node.getType().equals("bodywhile")||!last.getType().equals("bodyfor")||!last.getType().equals("bodyuntil"))
				{
					for(int i=getRootCfg().getGraphNodes().size()-1; i>0;i--)
					{
						if(getRootCfg().getGraphNodes().get(i).getType().equals("while"))
						{
							last.addOutgoingNodes(getRootCfg().getGraphNodes().get(i));
							break;
						}
						else if(getRootCfg().getGraphNodes().get(i).getType().equals("for"))
						{
							last.addOutgoingNodes(getRootCfg().getGraphNodes().get(i));
							break;
						}
						else if(getRootCfg().getGraphNodes().get(i).getType().equals("until"))
						{
							last.addOutgoingNodes(getRootCfg().getGraphNodes().get(i));
							break;
						}
					}
				}
			}
			else if(type=="when"||type=="elsecase")
			{
				if(last.getType()=="case")
				{
					node.addComingNodes(last);
					last.addOutgoingNodes(node);
				}
				for(int i=getRootCfg().getGraphNodes().size()-1; i>0;i--)
				{
					if(getRootCfg().getGraphNodes().get(i).getType().equals("case"))
					{
						node.addComingNodes(getRootCfg().getGraphNodes().get(i));
						getRootCfg().getGraphNodes().get(i).addOutgoingNodes(node);
					}
				}
			}
			else if(type=="then"||type=="else")
			{
				for(int i=getRootCfg().getGraphNodes().size()-1; i>0;i--)
				{
					if(getRootCfg().getGraphNodes().get(i).getType().equals("if"))
					{
						node.addComingNodes(getRootCfg().getGraphNodes().get(i));
						getRootCfg().getGraphNodes().get(i).addOutgoingNodes(node);
					}
				}
			}
			else if(type=="bodyfor"||type=="bodywhile"||type=="bodyuntil")
			{
				if(last.getType().equals("for"))
				{
					node.addComingNodes(last);
					last.addOutgoingNodes(node);
				}
				else if(last.getType().equals("while"))
				{
					node.addComingNodes(last);
					last.addOutgoingNodes(node);
				}
				else if(last.getType().equals("until"))
				{
					node.addComingNodes(last);
					last.addOutgoingNodes(node);
				}
				else
				{
					node.addComingNodes(last);
					last.addOutgoingNodes(node);
				}
			}
			if(type=="bodywhile"||type == "bodyfor"||type=="bodyuntil"||type=="statement"||type=="exit")
			{
				for(int i=getRootCfg().getGraphNodes().size()-1; i>0;i--)
				{
					if(getRootCfg().getGraphNodes().get(i).getType().equals("if") && getRootCfg().getGraphNodes().get(i).getOutgoingNodes().size()==1
							&& !getRootCfg().getGraphNodes().get(i).getOutgoingNodes().contains("else"))
					{
						getRootCfg().getGraphNodes().get(i).addOutgoingNodes(node);
						node.addComingNodes(getRootCfg().getGraphNodes().get(i));
					}
					else if(getRootCfg().getGraphNodes().get(i).getType().equals("case") && !getRootCfg().getGraphNodes().get(i).getOutgoingNodes().contains("elsecase"))
					{
						getRootCfg().getGraphNodes().get(i).addOutgoingNodes(node);
						node.addComingNodes(getRootCfg().getGraphNodes().get(i));
					}
				}
			}
			if(type=="exit")
			{
				if(last.getType().equals("statement"))
				{
					node.addComingNodes(last);
					last.addOutgoingNodes(node);
				}
				for(int i=getRootCfg().getGraphNodes().size()-1; i>0;i--)
				{
					for(CFGNode n:getRootCfg().getGraphNodes().get(i-1).getComingNodes())
					{
						if(getRootCfg().getGraphNodes().get(i).getComingNodes().contains(n.getComingNodes())&&getRootCfg().getGraphNodes().get(i).getOutgoingNodes()==null)
						{
							node.addComingNodes(getRootCfg().getGraphNodes().get(i));
							node.addComingNodes(getRootCfg().getGraphNodes().get(i-1));

							getRootCfg().getGraphNodes().get(i).addOutgoingNodes(node);
							break;
						}
						else
						{
							node.addComingNodes(getRootCfg().getGraphNodes().get(i));

							getRootCfg().getGraphNodes().get(i).addOutgoingNodes(node);
							break;
						}
					}
				}
			}
			
		}
		else
		{
			node.addComingNodes(last);
			last.addOutgoingNodes(node);
		}
	}

	@Override
	public Object visitAliasNode(AliasNode arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object visitAndNode(AndNode arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object visitArgsCatNode(ArgsCatNode arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object visitArgsNode(ArgsNode arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object visitArgsPushNode(ArgsPushNode arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object visitArgumentNode(ArgumentNode arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object visitArrayNode(ArrayNode arg0) {
		Node a = null ;
		a = arg0.get(0);
		return a;
	}

	@Override
	public Object visitAttrAssignNode(AttrAssignNode arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object visitBackRefNode(BackRefNode arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object visitBeginNode(BeginNode arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object visitBignumNode(BignumNode arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object visitBlockArg18Node(BlockArg18Node arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object visitBlockArgNode(BlockArgNode arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object visitBlockNode(BlockNode arg0) {
		for(int i=0; i<arg0.size(); i++)
		{
			visit(arg0.get(i));
		}
		return true;
	}

	@Override
	public Object visitBlockPassNode(BlockPassNode arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object visitBreakNode(BreakNode arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object visitCallNode(CallNode arg0) {
		if(arg0.getIter()!=null) visit(arg0.getIter());
		return true;
	}

	@Override
	public Object visitCaseNode(CaseNode arg0) {
		type="case";
		String adderCode = toCode(arg0.getCase())+"\n";
		code+=adderCode;
		cfgNode.setCode(code);
		cfgNode.setType(type);
		setIncomingNodes(cfgNode);
		getRootCfg().addNodes(cfgNode);
		
		if(arg0.getCases()!=null)
		{
			for(int i=0; i<arg0.getCases().size(); i++)
			{
				visit(arg0.getCases().get(i));
			}
		}
		if(arg0.getElse()!=null)
		{
			type="elsecase";
			cfgNode = new CFGNode();
			code="";
			visit(arg0.getElse());
		}
		return true;
	}

	@Override
	public Object visitClassNode(ClassNode arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object visitClassVarAsgnNode(ClassVarAsgnNode arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object visitClassVarDeclNode(ClassVarDeclNode arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object visitClassVarNode(ClassVarNode arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object visitColon2Node(Colon2Node arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object visitColon3Node(Colon3Node arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object visitCommentNode(CommentNode arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object visitConstDeclNode(ConstDeclNode arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object visitConstNode(ConstNode arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object visitDAsgnNode(DAsgnNode arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object visitDRegxNode(DRegexpNode arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object visitDStrNode(DStrNode arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object visitDSymbolNode(DSymbolNode arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object visitDVarNode(DVarNode arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object visitDXStrNode(DXStrNode arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object visitDefinedNode(DefinedNode arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object visitDefnNode(DefnNode arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object visitDefsNode(DefsNode arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object visitDotNode(DotNode arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object visitEncodingNode(EncodingNode arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object visitEnsureNode(EnsureNode arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object visitEvStrNode(EvStrNode arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object visitFCallNode(FCallNode arg0) {
		/*Node b = null;
		System.out.println(arg0.getArgs().getNodeType());
		if(arg0.getName().equals("resources"))
		{
			System.out.println(toCode(arg0.getArgs()));
			if(arg0.getArgs().getNodeType().equals(NodeType.ARRAYNODE))
				b = (Node) visitArrayNode((ArrayNode) arg0.getArgs());
			funcName = toCode(b);
			className = funcName+"_controller";
			component = "controller";

			//parent.addCallnodes(new CallsNode(funcName, className, component));
			
			System.out.println(funcName + " "+className+" "+component);
			if(arg0.getArgs().getNodeType().equals(NodeType.ITERNODE))
			{
				System.out.println("iter: "+toCode(arg0.getArgs()));
				visit(arg0.getArgs());
			}

			if(arg0.getIter()!=null)
			{
				visit(arg0.getIter());
			}
		}
		if(arg0.getArgs().getNodeType().equals(NodeType.ITERNODE))
		{
			visit(arg0.getArgs());
		}
		if(arg0.getArgs().getNodeType().equals(NodeType.ARRAYNODE))
		{
			b = (Node) visitArrayNode((ArrayNode) arg0.getArgs());
			System.out.println(toCode(b));
		}*/
		return null;
	}

	@Override
	public Object visitFalseNode(FalseNode arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object visitFixnumNode(FixnumNode arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object visitFlipNode(FlipNode arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object visitFloatNode(FloatNode arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object visitForNode(ForNode arg0) {
		type="for";
		String adderCode = toCode(arg0.getIter())+"\n";
		code+=adderCode;
		cfgNode.setCode(code);
		cfgNode.setType(type);
		setIncomingNodes(cfgNode);
		getRootCfg().addNodes(cfgNode);
		if(arg0.getBody()!=null)
		{
			type="bodyfor";
			cfgNode = new CFGNode();
			code = "";
			visit(arg0.getBody());
		}
		/*cfgNode=new CFGNode();
		code="";
		nodeAST="";*/
		return true;
	}

	@Override
	public Object visitGlobalAsgnNode(GlobalAsgnNode arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object visitGlobalVarNode(GlobalVarNode arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object visitHashNode(HashNode arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object visitIfNode(IfNode arg0) {
		type="if";
		String adderCode = toCode(arg0.getCondition())+"\n";
		code+=adderCode;
		cfgNode.setCode(code);
		cfgNode.setType(type);
		setIncomingNodes(cfgNode);
		getRootCfg().addNodes(cfgNode);
		if(arg0.getThenBody()!=null)
		{
			type="then";
			if(arg0.getThenBody().getNodeType().equals(NodeType.BLOCKNODE))
			{
				cfgNode = new CFGNode();
				code = "";
				visit(arg0.getThenBody());
			}
			else
			{
				cfgNode = new CFGNode();
				code = toCode(arg0.getThenBody())+"\n";
				//visit(arg0.getThenBody());
			}
			cfgNode.setCode(code);
			cfgNode.setType(type);
			setIncomingNodes(cfgNode);
			getRootCfg().addNodes(cfgNode);
		}
		if(arg0.getElseBody()!=null)
		{
			type="else";
			if(arg0.getElseBody().getNodeType().equals(NodeType.BLOCKNODE))
			{
				cfgNode = new CFGNode();
				code="";
				visit(arg0.getElseBody());
			}
			else
			{
			cfgNode = new CFGNode();
			code = toCode(arg0.getElseBody())+"\n";
			}
			cfgNode.setCode(code);
			cfgNode.setType(type);
			setIncomingNodes(cfgNode);
			getRootCfg().addNodes(cfgNode);
		}
		cfgNode=new CFGNode();
		code="";
		return true;
	}

	@Override
	public Object visitImplicitNilNode(ImplicitNilNode arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object visitInstAsgnNode(InstAsgnNode arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object visitInstVarNode(InstVarNode arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object visitIterNode(IterNode arg0) {
		visit(arg0.getBody());
		return true;
	}

	@Override
	public Object visitKeywordArgNode(KeywordArgNode arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object visitKeywordRestArgNode(KeywordRestArgNode arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object visitLambdaNode(LambdaNode arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object visitListNode(ListNode arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object visitLiteralNode(LiteralNode arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object visitLocalAsgnNode(LocalAsgnNode arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object visitLocalVarNode(LocalVarNode arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object visitMatch2Node(Match2Node arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object visitMatch3Node(Match3Node arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object visitMatchNode(MatchNode arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object visitMethodNameNode(MethodNameNode arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object visitModuleNode(ModuleNode arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object visitMultipleAsgnNode(MultipleAsgnNode arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object visitNewlineNode(NewlineNode arg0) {
		Node node = arg0.getNextNode();
		/*jika node berupa statement/assignment*/
		if(getRootCfg()!=null &&(node.getNodeType().equals(NodeType.ATTRASSIGNNODE)||node.getNodeType().equals(NodeType.BREAKNODE) ||
				node.getNodeType().equals(NodeType.CALLNODE) || node.getNodeType().equals(NodeType.CLASSVARASGNNODE)
						||node.getNodeType().equals(NodeType.DASGNNODE)||node.getNodeType().equals(NodeType.FCALLNODE)
						||node.getNodeType().equals(NodeType.GLOBALASGNNODE) || node.getNodeType().equals(NodeType.INSTASGNNODE)
						||node.getNodeType().equals(NodeType.LOCALASGNNODE)||node.getNodeType().equals(NodeType.MULTIPLEASGNNODE)
						||node.getNodeType().equals(NodeType.RETURNNODE)||node.getNodeType().equals(NodeType.VCALLNODE)))
		{
			code=toCode(node);
			if(code!="")
			{
			cfgNode.setCode(code);
			cfgNode.setType(type);
			setIncomingNodes(cfgNode);
			getRootCfg().addNodes(cfgNode);
			}
			type="exit";
			cfgNode = new CFGNode();
			cfgNode.setCode("Exit");
			cfgNode.setType(type);
			setIncomingNodes(cfgNode);
			getRootCfg().addNodes(cfgNode);
		}
		return true;
	}

	@Override
	public Object visitNextNode(NextNode arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object visitNilNode(NilNode arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object visitNotNode(NotNode arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object visitNthRefNode(NthRefNode arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object visitOpAsgnAndNode(OpAsgnAndNode arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object visitOpAsgnNode(OpAsgnNode arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object visitOpAsgnOrNode(OpAsgnOrNode arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object visitOpElementAsgnNode(OpElementAsgnNode arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object visitOptArgNode(OptArgNode arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object visitOrNode(OrNode arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object visitPostExeNode(PostExeNode arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object visitPreExeNode(PreExeNode arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object visitRedoNode(RedoNode arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object visitRegexpNode(RegexpNode arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object visitRescueBodyNode(RescueBodyNode arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object visitRescueNode(RescueNode arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object visitRestArgNode(RestArgNode arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object visitRetryNode(RetryNode arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object visitReturnNode(ReturnNode arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object visitRootNode(RootNode arg0) {
		setRootCfg(new ControlFlowGraph());
		cfgNode = new CFGNode();
		type="statement";
		code="";
		storage.addCfgFile("root", rootCfg);
		//setRootCfg(storage.getCfgFile().get("root"));
		visit(arg0.getBody());
		return arg0.getBody();
	}

	@Override
	public Object visitSClassNode(SClassNode arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object visitSValueNode(SValueNode arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object visitSelfNode(SelfNode arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object visitSplatNode(SplatNode arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object visitStrNode(StrNode arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object visitSuperNode(SuperNode arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object visitSymbolNode(SymbolNode arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object visitSyntaxNode(SyntaxNode arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object visitToAryNode(ToAryNode arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object visitTrueNode(TrueNode arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object visitUnaryCallNode(UnaryCallNode arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object visitUndefNode(UndefNode arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object visitUntilNode(UntilNode arg0) {
		type="until";
		String adderCode = toCode(arg0.getCondition())+"\n";
		code+=adderCode;
		cfgNode.setCode(code);
		cfgNode.setType(type);
		setIncomingNodes(cfgNode);
		getRootCfg().addNodes(cfgNode);
		if(arg0.getBody()!=null)
		{
			type="bodyuntil";
			cfgNode = new CFGNode();
			code="";
			visit(arg0.getBody());
		}
		/*cfgNode=new CFGNode();
		code="";
		nodeAST="";*/
		return true;
	}

	@Override
	public Object visitVAliasNode(VAliasNode arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object visitVCallNode(VCallNode arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object visitWhenNode(WhenNode arg0) {
		if(arg0!=null)
		{
			code="";
			type="when";
			//cfgNode = new CFGNode();
			visit(arg0.getExpression());
			code=toCode(arg0.getExpression())+"\n";
			/*cfgNode.setClassName(className);
			cfgNode.setMethodName(methodName);
			cfgNode.setMvc(mvc);
			cfgNode.setCode(code);
			if(!cfg.isExist(cfgNode))
				cfg.addNodes(cfgNode);
			code="";*/
			visit(arg0.getBody());
			cfgNode = new CFGNode();
			cfgNode.setCode(code);
			cfgNode.setType(type);
			setIncomingNodes(cfgNode);
			getRootCfg().addNodes(cfgNode);
		}
		return true;
	}

	@Override
	public Object visitWhileNode(WhileNode arg0) {
		type="while";
		String adderCode = toCode(arg0.getCondition())+"\n";
		code+=adderCode;
		
		cfgNode.setCode(code);
		cfgNode.setType(type);
		setIncomingNodes(cfgNode);
		getRootCfg().addNodes(cfgNode);
		if(arg0.getBody()!=null)
		{
			type="bodywhile";
			cfgNode = new CFGNode();
			code = "";
			visit(arg0.getBody());
		}
		/*cfgNode=new CFGNode();
		code="";
		nodeAST="";*/
		return true;
	}

	@Override
	public Object visitXStrNode(XStrNode arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object visitYieldNode(YieldNode arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object visitZArrayNode(ZArrayNode arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object visitZSuperNode(ZSuperNode arg0) {
		// TODO Auto-generated method stub
		return null;
	}

}
